
use ${newdata}carownership_dataset_bergen, clear

/* Only keep required obs */
keep if $trmgroup != 0 
keep if $trmgroup != .
keep if 	(year >= $firstyearpre  & year <= $lastyearpre) ///
		  | (year >= $firstyearpost & year <= $lastyearpost)

/* setup variables */
capt drop bergen 		  
gen commuters = ($trmgroup == 1 | $trmgroup == 3) 
gen bergen = 	($trmgroup == 1 | $trmgroup == 2)
gen post = (year >= $firstyearpost & year <= $lastyearpost)
gen bergen_commuters = bergen * commuters
gen commuters_post = commuters * post
gen bergen_commuters_post = bergen_commuters * post

/* =============================================================================
Only keeping 2014 and 2017 !!!
 =============================================================================*/
*keep if year == 2011 | year == $lastyearpre | year == $lastyearpost 
 
 
sort familienr year
egen long famid_num = group(familienr)


/* Import list of neighborhoods inside the toll cordon */

preserve 	
	import delimited ${additionaldata}bergen_center.csv, clear
	rename grunnkrets grk
	keep grk
	tempfile bergencenter
	save `bergencenter', replace	
	rename grk grk_work1
	tempfile bergencenter_w1
	save `bergencenter_w1', replace	
	rename grk_work1 grk_work2
	tempfile bergencenter_w2
	save `bergencenter_w2', replace
	
* Stavanger, Haugesund, Kristiansand	
	import delimited ${additionaldata}stavanger_center.csv, clear
	rename grunnkrets grk
	keep grk
	tempfile stavangercenter
	save `stavangercenter', replace
	rename grk grk_work1
	tempfile stavangercenter_w1
	save `stavangercenter_w1', replace	
	rename grk_work1 grk_work2
	tempfile stavangercenter_w2
	save `stavangercenter_w2', replace
	
	import delimited ${additionaldata}haugesund_center.csv, clear
	rename grunnkrets grk
	keep grk
	tempfile haugesundcenter
	save `haugesundcenter', replace
	rename grk grk_work1
	tempfile haugesundcenter_w1
	save `haugesundcenter_w1', replace	
	rename grk_work1 grk_work2
	tempfile haugesundcenter_w2
	save `haugesundcenter_w2', replace
	
	import delimited ${additionaldata}kristiansand_center.csv, clear
	rename grunnkrets grk
	keep grk
	tempfile kristiansandcenter
	save `kristiansandcenter', replace	
	rename grk grk_work1
	tempfile kristiansandcenter_w1
	save `kristiansandcenter_w1', replace	
	rename grk_work1 grk_work2
	tempfile kristiansandcenter_w2
	save `kristiansandcenter_w2', replace	
restore	

capt drop insidetc 
capt drop aux

gen insidetc = 0 
gen insidetc_w1 = 0
gen insidetc_w2 = 0
* Living inside toll cordon
merge m:1 grk using `bergencenter', keep(match master)
replace insidetc = 1 if _merge == 3
drop _merge 
merge m:1 grk using `stavangercenter', keep(match master)
replace insidetc = 1 if _merge == 3
drop _merge
merge m:1 grk using `haugesundcenter', keep(match master)
replace insidetc = 1 if _merge == 3
drop _merge
merge m:1 grk using `kristiansandcenter', keep(match master)
replace insidetc = 1 if _merge == 3
drop _merge
* Working inside toll cordon
merge m:1 grk_work1 using `bergencenter_w1', keep(match master)
replace insidetc_w1 = 1 if _merge == 3
drop _merge
merge m:1 grk_work1 using `stavangercenter_w1', keep(match master)
replace insidetc_w1 = 1 if _merge == 3
drop _merge
merge m:1 grk_work1 using `haugesundcenter_w1', keep(match master)
replace insidetc_w1 = 1 if _merge == 3
drop _merge
merge m:1 grk_work1 using `kristiansandcenter_w1', keep(match master)
replace insidetc_w1 = 1 if _merge == 3
drop _merge

merge m:1 grk_work2 using `bergencenter_w2', keep(match master)
replace insidetc_w2 = 1 if _merge == 3
drop _merge
merge m:1 grk_work2 using `stavangercenter_w2', keep(match master)
replace insidetc_w2 = 1 if _merge == 3
drop _merge
merge m:1 grk_work2 using `haugesundcenter_w2', keep(match master)
replace insidetc_w2 = 1 if _merge == 3
drop _merge
merge m:1 grk_work2 using `kristiansandcenter_w2', keep(match master)
replace insidetc_w2 = 1 if _merge == 3
drop _merge

gen aux = insidetc 
replace aux = . if year != $lastyearpre
bys famid_num: egen inside_2014 = mean(aux)
drop aux

gen aux = insidetc_w1 
replace aux = . if year != $lastyearpre
bys famid_num: egen inside_w1_2014 = mean(aux)
drop aux

gen aux = insidetc_w2 
replace aux = . if year != $lastyearpre 
bys famid_num: egen inside_w2_2014 = mean(aux)
drop aux

gen inside_woutside = (insidetc == 1 & ((insidetc_w1 == 0 & grk_bed1 != "")|(insidetc_w1 == 0 & grk_bed1 != "")))
gen outside_winside = (insidetc == 0 & ((insidetc_w1 == 1 & grk_bed1 != "")|(insidetc_w1 == 1 & grk_bed1 != "")))

* Moving inside
gen movinginside = 0
bys familienr (year): replace movinginside = 1 if insidetc[_n] == 1 & insidetc[_n - 1] == 0 & familienr[_n] == familienr[_n-1]

* Moving outside
gen movingoutside = 0
bys familienr (year): replace movingoutside = 1 if insidetc[_n] == 0 & insidetc[_n - 1] == 1 & familienr[_n] == familienr[_n-1]

* workplace realocation inside (any adult)
gen workrealocinside = 0
bys familienr (year): replace workrealocinside = 1 ///
	if ((insidetc_w1[_n] == 1 & insidetc_w1[_n - 1] == 0) |  (insidetc_w2[_n] == 1 & insidetc_w2[_n - 1] == 0)) ///
	& familienr[_n] == familienr[_n-1]

	
* workplace realocation inside (any adult)
gen workrealocoutside = 0
bys familienr (year): replace workrealocoutside = 1 ///
	if ((insidetc_w1[_n] == 0 & insidetc_w1[_n - 1] == 1) |  (insidetc_w2[_n] == 0 & insidetc_w2[_n - 1] == 1)) ///
	& familienr[_n] == familienr[_n-1]
	
	
tab inside_2014 inside_w1_2014 
tab inside_2014 inside_w2_2014

* ===

capt drop mover
gen mover = 0
* Family changed residence
bys familienr (year): replace mover = 1 ///
	if (grk[_n] != grk[_n-1]) & familienr[_n] == familienr[_n-1]

* Family changed residence inside toll cordon
gen mover_inside = 0
bys familienr (year): replace mover_inside = 1 ///
	if (grk[_n] != grk[_n-1]) & insidetc[_n] == 1 & familienr[_n] == familienr[_n-1]	
	
* Family changed residence outside toll cordon
gen mover_outside = 0
bys familienr (year): replace mover_outside = 1 ///
	if (grk[_n] != grk[_n-1]) & insidetc[_n] == 0 & familienr[_n] == familienr[_n-1]		

* Changing moving status from inside to outside or outside to inside	
gen mover2 = 0
bys familienr (year): replace mover2 = 1 ///
	if (insidetc[_n] != insidetc[_n-1] & familienr[_n] == familienr[_n-1])
		
gen workdistance = dist

replace grk_work1 = 0 if grk_work1 == .
replace grk_work2 = 0 if grk_work2 == .
gen jobchange = 0
bys familienr (year): replace jobchange = 1 ///
	if ((grk_work1[_n] != grk_work1[_n-1] ) ///
	| (grk_work2[_n] != grk_work2[_n-1] ) ) /// 
	& familienr[_n] == familienr[_n-1]
	
* All time-varying variables based on geography (residence or work location)
foreach var in dist time_work PublicVSCarTime_fam_mean PublicDiffCarTime_fam_mean grk {
	replace `var' = . if year != 2014
	bysort familienr (`var'): replace `var' = `var'[_n-1] if missing(`var')
}
		
*-------------------------------------------------------------------------------	
* Regressions
*-------------------------------------------------------------------------------	

gen movingany = movinginside + movingoutside 
tab year movingany 

* === REG 1A: This or "mover"

reghdfe mover ///
	bergen_commuters_post /// DiDiD: post commuter in bergen	
	$xvar /// 
	i.commuters#i.year /// 
	bergen_commuters /// 
	, absorb( ///
		i.grk#i.year ///
		i.fam_fe ///
	) vce(cluster grk)	
	
summarize mover if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
estadd scalar MeanValue1 = r(mean), replace
summarize mover if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
estadd scalar MeanValue2 = r(mean), replace
estadd local YearGrkFe = "\checkmark", replace
eststo reg_1 	
estimates save ${ster}tab4_resloc_col1, replace


* === REG 1B: Any job location change
reghdfe jobchange ///
	bergen_commuters_post /// DiDiD: post commuter in bergen	
	$xvar /// 
	i.commuters#i.year /// 
	bergen_commuters /// 
	, absorb( ///
		i.grk#i.year ///
		i.fam_fe ///
	) vce(cluster grk)	
	
summarize jobchange if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
estadd scalar MeanValue1 = r(mean), replace
summarize jobchange if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
estadd scalar MeanValue2 = r(mean), replace
estadd local YearGrkFe = "\checkmark", replace
eststo reg_1 	
estimates save ${ster}tab4_wploc_col1, replace

* === REG 2A: Moving inside the toll cordon	
reghdfe movinginside ///
	bergen_commuters_post /// DiDiD: post commuter in bergen	
	$xvar /// 
	i.commuters#i.year /// 
	bergen_commuters /// 
	, absorb( ///
		i.grk#i.year ///
		i.fam_fe ///
	) vce(cluster grk)

summarize movinginside if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
estadd scalar MeanValue1 = r(mean), replace
summarize movinginside if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
estadd scalar MeanValue2 = r(mean), replace
estadd local YearGrkFe = "\checkmark", replace
eststo reg_1 	
estimates save ${ster}tab4_resloc_col2, replace
	
* === REG 2B: Realoccating workplace to inside the toll cordon	
reghdfe workrealocinside ///
	bergen_commuters_post /// DiDiD: post commuter in bergen	
	$xvar /// 
	i.commuters#i.year /// 
	bergen_commuters /// 
	, absorb( ///
		i.grk#i.year ///
		i.fam_fe ///
	) vce(cluster grk)	

summarize workrealocinside if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
estadd scalar MeanValue1 = r(mean), replace
summarize workrealocinside if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
estadd scalar MeanValue2 = r(mean), replace
estadd local YearGrkFe = "\checkmark", replace
eststo reg_1 	
estimates save ${ster}tab4_wploc_col2, replace
	
	
* === REG 4A: Moving outside the toll cordon	
reghdfe movingoutside ///
	bergen_commuters_post /// DiDiD: post commuter in bergen	
	$xvar /// 
	i.commuters#i.year /// 
	bergen_commuters /// 
	, absorb( ///
		i.grk#i.year ///
		i.fam_fe ///
	) vce(cluster grk)

summarize movingoutside if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
estadd scalar MeanValue1 = r(mean), replace
summarize movingoutside if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
estadd scalar MeanValue2 = r(mean), replace
estadd local YearGrkFe = "\checkmark", replace
eststo reg_1 	
estimates save ${ster}tab4_resloc_col3, replace
	
* === REG 4B: Realoccating workplace to outside the toll cordon	
reghdfe workrealocoutside ///
	bergen_commuters_post /// DiDiD: post commuter in bergen	
	$xvar /// 
	i.commuters#i.year /// 
	bergen_commuters /// 
	, absorb( ///
		i.grk#i.year ///
		i.fam_fe ///
	) vce(cluster grk)	
	
summarize workrealocoutside if e(sample) == 1 & year == $lastyearpre & bergen_commuters == 1
estadd scalar MeanValue1 = r(mean), replace
summarize workrealocoutside if e(sample) == 1 & year == $lastyearpost & bergen_commuters == 1
estadd scalar MeanValue2 = r(mean), replace
estadd local YearGrkFe = "\checkmark", replace
eststo reg_1 	
estimates save ${ster}tab4_wploc_col3, replace
	

	

* == Making table
eststo drop *
foreach var in _resloc_col1 _resloc_col2 _resloc_col3 _wploc_col1 _wploc_col2 _wploc_col3 {
	estimates use "${ster}tab4`var'"
	eststo
}

esttab *, keep(bergen_commuters_post)  ///
	stats(N  MeanValue1, /// MeanValue2, ///
	label("Observations"  "Mean depvar 2014 (paying commuters, Bergen)" /// "Mean depvar 2017 (paying commuters, Bergen)" ///
	) fmt(0 4) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01) 
	
	
* == Saving table
eststo drop *
foreach var in _resloc_col1 _resloc_col2 _resloc_col3  {
	estimates use "${ster}tab4`var'"
	eststo
}	
	
esttab * ///
	using "${tables}tab4a_est.tex", ///
	keep(bergen_commuters_post)  ///
	coeflabels( ///
	bergen_commuters_post "Post $\times$ Paying commuters $\times$ Bergen" ///
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs /// label   nogap booktabs ///
	nomtitle /// 
	prehead("") posthead("") ///
	stats() ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

esttab * ///
	using "${tables}tab4a_sumstat.tex", ///
	drop(*)  ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs /// label   nogap booktabs ///
	nomtitle  /// 
	prehead("") posthead("") ///
	stats(N  MeanValue1, /// MeanValue2, ///
	label("Observations"  "Mean depvar 2014 (paying commuters, Bergen)" /// "Mean depvar 2017 (paying commuters, Bergen)" ///
	) fmt(0 4) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)	
	
eststo drop *
foreach var in _wploc_col1 _wploc_col2 _wploc_col3  {
	estimates use "${ster}tab4`var'"
	eststo
}	
	
esttab * ///
	using "${tables}tab4b_est.tex", ///
	keep(bergen_commuters_post)  ///
	coeflabels( ///
	bergen_commuters_post "Post $\times$ Paying commuters $\times$ Bergen"  ///
	) ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs /// label   nogap booktabs ///
	nomtitle /// 
	prehead("") posthead("") ///
	stats() ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)

esttab * ///
	using "${tables}tab4b_sumstat.tex", ///
	drop(*)  ///
	fragment nodepvars nonumbers nogaps nolines nonotes noobs /// label   nogap booktabs ///
	nomtitle  /// 
	prehead("") posthead("") ///
	stats(N  MeanValue1, /// MeanValue2, ///
	label("Observations"  "Mean depvar 2014 (paying commuters, Bergen)" /// "Mean depvar 2017 (paying commuters, Bergen)" ///
	) fmt(0 4) ) ///
	se  ///
	star(* 0.10 ** 0.05 *** 0.01)  ///
	replace	substitute(\_ _)	
	